﻿function AcceptOnlyNumbers(e)
{
	var keynum;
	var keychar;
	var numcheck;
	if(window.event) // IE
	{
	  keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
	  keynum = e.which;
	}
	//keynum :   "/" = 47,  "0" = 48, "1" = 49, "2" = 50 etc  "9" = 57
	//alert(keynum);
	
	//"," = 44, "." = 46
	if (keynum == 44 || keynum == 46)
	{
		return true;
	}
	
	if (keynum < 47 || keynum > 57)
	{
		return false;
	}
	return true;
}

function DepartementChanged(ctl)
{
	var frm = document.forms[0];

	try
	{
		DeleteOptions(frm.OwnerServiceDSI);
		
		var depCrt = frm.OwnerDepartmentDSI.value;
		var idDepCrt = "";
		for (var i=0; i < arrDepLb.length; i++)
		{
			if (arrDepLb[i] == depCrt)
			{
				idDepCrt = arrDepId[i];
				break;
			}
		}
		if (idDepCrt == "")
		{
			return;
		}
		
		var arrId = eval("arrServiceLb_" + idDepCrt);
		var arrLb = arrId;

		AddOptionsFromArrayWithFirstOption(frm.OwnerServiceDSI, arrId, arrLb, "");
	}
	catch (ex) {};	
}

function ProjectChangedDeptService(ctl)
{
	var frm = document.forms[0];
	try
	{
		var deptCrt = "";
		var serviceCrt = "";
		
		var prjCrt = "";
		if (frm.Projet) //fiche tâche
		{
			prjCrt = frm.Projet.value;
			return;
		}
		else //fiche événement
		{
			prjCrt = frm["Libellé du projet"].value;
		}
		//alert("prjCrt : " + prjCrt);
		var idPrjCrt = "";
		for (var i=0; i < arrPrjLabel.length; i++)
		{
			if (arrPrjLabel[i] == prjCrt)
			{
				deptCrt = arrPrjDept[i];
				serviceCrt = arrPrjService[i];
				//alert("deptCrt : " + deptCrt);
				//alert("serviceCrt : " + serviceCrt);
				break;
			}
		}
		if (deptCrt == "")
		{
			return;
		}
		frm.OwnerDepartmentDSI.value = deptCrt;
		DepartementChanged(frm.OwnerDepartmentDSI);
		frm.OwnerServiceDSI.value = serviceCrt;		
	}
	catch (ex) {};
}

function ProjectChanged(ctl)
{
	var frm = document.forms[0];
	ProjectChangedDeptService(ctl);
	try
	{
		DeleteOptions(frm.Tache);
		
		var prjCrt = frm.Projet.value;
		var idPrjCrt = "";
		for (var i=0; i < arrPrjLb.length; i++)
		{
			if (arrPrjLb[i] == prjCrt)
			{
				idPrjCrt = arrPrjId[i];
				break;
			}
		}
		if (idPrjCrt == "")
		{
			return;
		}		
		var arrId = eval("arrTacheLb_" + idPrjCrt);
		var arrLb = arrId;

		AddOptionsFromArrayWithFirstOption(frm.Tache, arrId, arrLb, "");
	}
	catch (ex) {};
}

//Add options from array to a select element
function AddOptionsFromArrayWithFirstOption(dst, arrId, arrLb, optionStart)
{
	dst.options.length = 0;	
	AddOption(dst, optionStart, optionStart);
	for (var i=0; i < arrId.length; i++)
	{
		AddOption(dst, arrLb[i], arrId[i]);
	}
}

//Add option to select or multi-select
function AddOption(cbo, id, lb)
{
	for (var i=0; i < cbo.length; i++)
	{
		if (cbo[i].value == id)
		{
			return;
		}
	}
	var j = cbo.length;
	cbo.options[j] = new Option(id, lb);
}

// Retire options from multi-select
function DeleteOptions(sFrom)
{
	if (((i = (a=sFrom.options).selectedIndex)>=0) && (sFrom.options.length>0)) 
	{
		a[i]=null;
		DeleteOptions(sFrom);
	}
}

var mess = "";
function CheckRequired()
{
	var frm = document.forms[0];
	mess = "";
	CheckControl(frm.Titre, "Titre");
	CheckControl(frm.Projet, "Projet");
	CheckControl(frm["Libellé du projet"], "Libellé du projet");
	CheckControl(frm["Phase Projet"], "Phase Projet");
	CheckControl(frm["Budget Initial"], "Budget Initial");
	
	if (mess == "")
	{
		return true;
	}
	else
	{
		alert(mess);
		return false;
	}
}
function CheckControl(ctl, nom)
{
	if (ctl)
	{
		if (ctl.value == "")
		{
			mess += nom + " obligatoire\n";
		}
	}
}